<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="no.ntnu.tdt4237.*" %>
    <%@page import="no.ntnu.tdt4237.helperactions.*"%>
    <%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<tags:header title=" - Log In"></tags:header>


		

<script language="JavaScript" type="text/JavaScript">
	function redirect() {
		var loc = location.href.split(':');
		if (loc[0]=='http') {
			location.href='https:'+loc[1].substring(0,loc[1].length-1)+":8453";
		}
	}
</script>

<body onload="redirect()">
<%


User loggedInUser = Authentication.logIn(request.getParameter("UserName"), request.getParameter("Password"));
	
if (loggedInUser != null) {
	
	if(Database.validUser(request.getParameter("UserName"))){
		//user is logged in.¨
		session.setAttribute(SessionKeys.USER_OBJECT, loggedInUser);
		session.setMaxInactiveInterval(600);
	}
	else{
		loggedInUser = null;
		%>
		<tags:message message="Account not validated!"></tags:message>
		<span><a href="Activation.jsp">Activate account!</a></span>
		<%
	}

	
	%>
	<tags:menu loggedInUser="<%=loggedInUser%>"></tags:menu>
	<%
} else {
	if (request.getParameter("Password") != null) {
		%>
		<tags:error errorMsg="You are not logged in"></tags:error>
		<%
	}
	%>
	<tags:menu loggedInUser="<%=null%>"></tags:menu>
	<tags:loginbox></tags:loginbox>
	<% 
}
%>
</body>
</html>